﻿using MySqlConnector;

namespace Front._1124;

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        Console.InputEncoding = System.Text.Encoding.UTF8;
        //实现登录逻辑
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        //获取窗体上的账号和密码
        string account = this.textBox1.Text;
        string password = this.textBox2.Text;
        
        string pwd = MD5Helper.GetMD5(password);
        
        string sql = "SELECT * FROM admin_info where admin_account = @account and admin_password = @pwd";

        using var command = new MySqlCommand(sql, connection);
        //设置参数
        command.Parameters.AddWithValue("account", account);
        command.Parameters.AddWithValue("pwd", pwd);
        
        
        using var reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            //跳转到主窗体
            MainForm mainForm = new MainForm();
            mainForm.Show();
            //隐藏登录窗体
            this.Hide();
        }
        else
        {
            MessageBox.Show("用户名或密码错误");
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }
        // while (reader.Read())
        //     Console.WriteLine(reader.GetString(2));
    }

    private void Login_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = "admin_super01";
        this.textBox2.Text = "123456";
    }
}